<?php

namespace App\Http\Controllers;

use Exception;
use App\Models\Company;
use App\Models\System\SysLog;
use Illuminate\Support\Facades\DB;
use App\Repositories\UserRepository;
use Illuminate\Support\Facades\Auth;
use App\Repositories\CompanyRepository;
use App\Http\Requests\Logic\Company\SyncModules;
use App\Http\Requests\Logic\Company\StoreResource;
use App\Http\Requests\Logic\Company\QueryResource;
use App\Http\Requests\Logic\Company\UpdateResource;
use App\Http\Requests\Logic\Company\DestroyResource;
use App\Http\Requests\Logic\Company\QueryCollection;

/**
 * 公司管理控制器
 *
 * Date: 22/03/2018
 * @author George
 * @package App\Http\Controllers
 */
class CompanyController extends Controller
{
	protected $userRepository;
	protected $companyRepository;

	/**
	 * CompanyController constructor.
	 * @param UserRepository $userRepository
	 * @param CompanyRepository $companyRepository
	 */
	public function __construct(
		UserRepository $userRepository,
		CompanyRepository $companyRepository
	)
	{
		$this->companyRepository = $companyRepository;
		$this->userRepository = $userRepository;
	}

	/**
	 * 获取公司信息集合
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param QueryCollection $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function index(QueryCollection $request)
	{
		$status = $request->get('status');
		$paginate = $request->get('paginate', 15);
		if ($status) {
			$this->companyRepository->where('status', '=', $status);
		}

		return success($this->companyRepository->paginate($paginate));
	}

	/**
	 * 获取根据主键公司信息
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param QueryResource $request
	 * @param Company $company
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function show(QueryResource $request, Company $company)
	{
		return success($company);
	}

	/**
	 * 创建公司
	 *
	 * Date: 2018/5/24
	 * @author George
	 * @param StoreResource $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
	public function store(StoreResource $request)
	{
		$attributes = $this->extractInputFromRules($request, $request->rules());

		$attributes['creator_id'] = Auth::id();

		try {
			DB::beginTransaction();
			$company = Company::create($attributes);
			$user = Auth::user();
			$user->company_id = $company->id;
			$user->saveOrFail();
            SysLog::log($user,'公司模块','创建公司');
            DB::commit();
			return stored($company, '创建成功，请等待审核');
		} catch (Exception $exception) {
			DB::rollBack();
			return internalError('创建公司失败', 500);
		}
	}

	/**
	 * 更新公司信息
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param UpdateResource $request
	 * @param Company $company
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function update(UpdateResource $request, Company $company)
	{
		$attributes = $this->extractInputFromRules($request, $request->rules());
		try {
            SysLog::log(Auth::user(),'公司模块',"更新公司信息");
			$company->update($attributes);
			return updated($company);
		} catch (Exception $exception) {
			return internalError();
		}
	}

	/**
	 * 删除公司信息
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param DestroyResource $request
	 * @param Company $company
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \App\Exceptions\GeneralException
	 */
	public function destroy(DestroyResource $request, Company $company)
	{
		$this->companyRepository->destroy($company);
        SysLog::log(Auth::user(),'公司模块',"删除公司信息");
		return deleted();
	}

	/**
	 * 获取公司模块信息
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param Company $company
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function modules(Company $company)
	{
		$modules = $this->companyRepository->getModules($company);

		return success($modules);
	}

	/**
	 * 同步公司模块绑定关系
	 *
	 * Date: 25/03/2018
	 * @author George
	 * @param SyncModules $request
	 * @param Company $company
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function syncModules(SyncModules $request, Company $company)
	{
		$modules = $request->get('modules');
		try {
			$company->modules()->sync($modules);
			return success($modules);
		} catch (Exception $exception) {
			return internalError();
		}
	}
}
